package com.base.controller;


import cn.dev33.satoken.util.SaResult;
import com.base.entity.Room;
import com.base.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-11-29
 */
@Controller  // 使用@Controller来处理视图跳转
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private IRoomService roomService;

    // 获取房间信息
    @GetMapping("/get/{id}")
    @ResponseBody // 强制返回JSON格式
    public SaResult getRoomById(@PathVariable Integer id) {
        Room room = roomService.getById(id);
        return SaResult.data(room);
    }

    // 删除房间
    @GetMapping("/remove/{id}")
    @ResponseBody // 强制返回JSON格式
    public SaResult removeRoomById(@PathVariable Integer id) {
        roomService.removeById(id);
        return SaResult.ok();
    }

    // 添加或更新房间
    @PostMapping
    @ResponseBody // 强制返回JSON格式
    public SaResult addRoom(Room room) {
        if (room.getId() == null) {
            room.setIsFree(1);
            roomService.save(room);
            return SaResult.ok();
        } else {
            roomService.updateById(room);
            return SaResult.ok();
        }
    }


}
